<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2017/10/30
 * Time: 上午10:01
 */

namespace common\helpers;

use Yii;
use yii\caching\Dependency;

class CacheHelper
{
    /**
     * 设置缓存
     * @param $key
     * @param $data
     * @param null $dependencyOrDuration
     */
    public static function setCache($key, $data, $dependencyOrDuration = null) {
        if($dependencyOrDuration instanceof Dependency) {
            Yii::$app->cache->set($key, $data, CACHE_DURATION, $dependencyOrDuration);
        }else if (is_numeric($dependencyOrDuration)) {
            Yii::$app->cache->set($key, $data, $dependencyOrDuration);
        }else {
            Yii::$app->cache->set($key, $data);
        }
    }
    
    /**
     * 读取缓存
     * @param $key
     * @return mixed
     */
    public static function getCache($key) {
        return Yii::$app->cache->get($key);
    }
    
    /**
     * 删除缓存
     * @param $key
     */
    public static function removeCache($key) {
        Yii::$app->cache->delete($key);
    }
    
}